<?php

/**
 * @author Petr Vostiňák
 */

class Admin_LokaceController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $lokace = new Knihovna_Model_DbTable_Lokace();
        $this->view->lokace = $lokace->fetchAll();
        $flashMessage = $this->_helper->flashMessenger;
        $this->view->message = $flashMessage->getMessages();
    }

    public function addAction()
    {
        $form = new Admin_Form_Lokace();
        $form->submit->setLabel('Add');
        $this->view->form = $form;
        if ($this->getRequest()->isPost())
        {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData))
            {
                $mesto = $form->getValue('mesto');
                $ulice = $form->getValue('ulice');
                $lokace = new Knihovna_Model_DbTable_Lokace();
                $lokace->addLokace($mesto, $ulice);
                $this->_helper->redirector('index');
            }
            else
            {
                $form->populate($formData);
            }
        }
    }

    public function editAction()
    {
        $form = new Admin_Form_Lokace();
        $form->submit->setLabel('Uložit');
        $this->view->form = $form;
        if ($this->getRequest()->isPost())
        {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData))
            {
                $id_lokace = (int)$form->getValue('id_lokace');
                $mesto = $form->getValue('mesto');
                $ulice = $form->getValue('ulice');

                $lokace = new Knihovna_Model_DbTable_Lokace();
                $lokace->editLokace($id_lokace, $mesto, $ulice);
                $this->_helper->redirector('index');
            }
            else
            {
                $form->populate($formData);
            }
        }
        else
        {
            $id_lokace = $this->_getParam('id_lokace', 0);
            if ($id_lokace > 0)
            {
                $lokace = new Knihovna_Model_DbTable_Lokace();
                $form->populate($lokace->getLokace($id_lokace));
            }
        }
    }

   public function deleteAction()
    {
        $form = new Admin_Form_DeleteLokace();

        if ($this->getRequest()->isPost() && (false !== $this->getRequest()->getPost('yes', false))) {
            try{
                $id_lokace = $this->_getParam('id_lokace');
                $lokace = new Knihovna_Model_DbTable_Lokace();
                $lokace ->deleteLokace($id_lokace);
                $this->_helper->flashMessenger('success|Lokace byla úspěšně smazána.');
                $this->_helper->redirector('index');
            }catch(Zend_Db_Exception $error){
                $this->_helper->flashMessenger('error|'.$error->getMessage());
            }
        }elseif($this->getRequest()->isPost() && (false !== $this->getRequest()->getPost('no', false))){
            $this->_redirect('/admin/users/');
        }

        $this->view->form = $form;
    }
}